using System;
using System.Collections.Generic;
using UnityEngine;

public class TransparencyManager : MonoBehaviour
{
	[Serializable]
	public class EditorReferences
	{
		public Material transparentMaterial;
	}

	public static TransparencyManager Instance;

	[SerializeField]
	public EditorReferences references = new EditorReferences();

	private Camera _mainCamera;

	private Transform _target;

	private List<FallingObjectTransparency> _currentTransparentObjects = new List<FallingObjectTransparency>();

	public Material TransparentMaterial => references.transparentMaterial;

	private void Awake()
	{
		Instance = this;
		_mainCamera = Camera.main;
	}

	public void SetTarget(Transform target)
	{
		_target = target;
	}

	private void Update()
	{
		if (!_target)
		{
			return;
		}
		Vector3 b = _target.position - _mainCamera.transform.position;
		float maxDistance = b.magnitude * 1.8f;
		RaycastHit[] array = Physics.RaycastAll(_mainCamera.transform.position - b, b.normalized, maxDistance);
		foreach (FallingObjectTransparency currentTransparentObject in _currentTransparentObjects)
		{
			currentTransparentObject.IsRaycasted = false;
		}
		if (array.Length > 0)
		{
			RaycastHit[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				RaycastHit raycastHit = array2[i];
				if (!raycastHit.collider.CompareTag("FallingObject"))
				{
					continue;
				}
				FallingObject componentInParent = raycastHit.collider.GetComponentInParent<FallingObject>();
				if (componentInParent.CanBeTransparent)
				{
					FallingObjectTransparency fallingObjectTransparency = componentInParent.GetComponent<FallingObjectTransparency>();
					if (!fallingObjectTransparency)
					{
						fallingObjectTransparency = componentInParent.gameObject.AddComponent<FallingObjectTransparency>();
						fallingObjectTransparency.Init();
					}
					if (!_currentTransparentObjects.Contains(fallingObjectTransparency))
					{
						_currentTransparentObjects.Add(fallingObjectTransparency);
					}
					fallingObjectTransparency.IsRaycasted = true;
					fallingObjectTransparency.SetTransparent(state: true);
				}
			}
		}
		foreach (FallingObjectTransparency currentTransparentObject2 in _currentTransparentObjects)
		{
			if (!currentTransparentObject2.IsRaycasted)
			{
				currentTransparentObject2.SetTransparent(state: false);
			}
		}
		_currentTransparentObjects.RemoveAll((FallingObjectTransparency item) => !item.IsRaycasted);
	}
}
